package com.zbkj.client.controller;

import com.zbkj.client.config.ClientTokenComponent;
import com.zbkj.client.service.ContactsService;
import com.zbkj.common.model.client.discovery.Contacts;
import com.zbkj.common.request.client.BatchDeleteRequest;
import com.zbkj.common.request.client.ContactsAddRequest;
import com.zbkj.common.response.client.ContactsListResponse;
import com.zbkj.common.result.CommonResult;
import com.zbkj.common.response.ClientLoginResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "用户通讯录")
@RestController
@RequestMapping("api/client/contacts")
public class ContactsController {

    @Autowired
    private ContactsService contactsService;

    @Autowired
    private ClientTokenComponent clientTokenComponent;

    @ApiOperation(value = "获取用户通讯录列表")
    @GetMapping("/getList")
    public CommonResult<List<ContactsListResponse>> getList(
            @ApiParam(value = "父级ID", required = false) @RequestParam(value = "parentId", defaultValue = "0") Integer parentId, HttpServletRequest request) {

        Integer userId = clientTokenComponent.getLoginUser(request);
        if (userId == null) {
            return com.zbkj.common.result.CommonResult.failed("用户未登录");
        }
        
//        List<Contacts> contactsList = contactsService.getContactsListByUserId(parentId, userId);
        List<ContactsListResponse> contactsList = contactsService.getContactsListWithHeadImg(parentId, userId);
        return CommonResult.success(contactsList);
    }

    @ApiOperation(value = "添加到通讯录")
    @PostMapping("/addContact")
    public CommonResult<Map<String, Object>> addContact(
            @RequestBody ContactsAddRequest contactsAddRequest,
            HttpServletRequest request) {

        Integer userId = clientTokenComponent.getLoginUser(request);
        if (userId == null) {
            return CommonResult.failed("用户未登录");
        }

        Integer parentId = contactsAddRequest.getParentId() == null ? 0 : contactsAddRequest.getParentId();
        String agentId = contactsAddRequest.getAgentId();
        if (agentId == null) {
            return CommonResult.failed("请选择要传入的代理");
        }

        String token = request.getHeader("token");
        ClientLoginResponse userInfo = clientTokenComponent.getUserInfo(userId, token);
        if (userInfo == null) {
            return CommonResult.failed("用户信息获取失败");
        }

        Integer merId = userInfo.getMerId();

        String type = contactsAddRequest.getType();
        String name = contactsAddRequest.getName();

        boolean result = contactsService.addContact(parentId, agentId, userId, merId, type, name);
        if (result) {
            Map<String, Object> data = new HashMap<>();
            data.put("result", 1);

            return CommonResult.success(data);
        } else {
            return CommonResult.failed("添加失败");
        }
    }

    @ApiOperation(value = "新建通讯录分组")
    @PostMapping("/addGroupContact")
    public CommonResult<Map<String, Object>> addGroupContact(
            @RequestBody ContactsAddRequest contactsAddRequest,
            HttpServletRequest request) {

        Integer userId = clientTokenComponent.getLoginUser(request);
        if (userId == null) {
            return CommonResult.failed("用户未登录");
        }

        Integer parentId = contactsAddRequest.getParentId() == null ? 0 : contactsAddRequest.getParentId();
        String name = contactsAddRequest.getName();
        if (name == null) {
            return CommonResult.failed("请输入分组名称");
        }

        String token = request.getHeader("token");
        ClientLoginResponse userInfo = clientTokenComponent.getUserInfo(userId, token);
        if (userInfo == null) {
            return CommonResult.failed("用户信息获取失败");
        }

        Integer merId = userInfo.getMerId();


        boolean result = contactsService.addGroupContact(parentId, name, userId, merId);
        if (result) {
            Map<String, Object> data = new HashMap<>();
            data.put("result", 1);

            return CommonResult.success(data);
        } else {
            return CommonResult.failed("分组创建失败");
        }
    }

    @ApiOperation(value = "移动通讯录")
    @PostMapping("/moveGroupContact")
    public CommonResult<Map<String, Object>> moveGroupContact(
            @RequestBody ContactsAddRequest contactsAddRequest,
            HttpServletRequest request) {

        Integer userId = clientTokenComponent.getLoginUser(request);
        if (userId == null) {
            return CommonResult.failed("用户未登录");
        }

        Integer parentId = contactsAddRequest.getParentId() == null ? 0 : contactsAddRequest.getParentId();
        Integer id = contactsAddRequest.getId();
        if (id == null) {
            return CommonResult.failed("请选择要移动的通讯录名字");
        }

        String token = request.getHeader("token");
        ClientLoginResponse userInfo = clientTokenComponent.getUserInfo(userId, token);
        if (userInfo == null) {
            return CommonResult.failed("用户信息获取失败");
        }

        boolean result = contactsService.moveGroupContact(parentId, id, userId);
        if (result) {
            Map<String, Object> data = new HashMap<>();
            data.put("result", 1);

            return CommonResult.success(data);
        } else {
            return CommonResult.failed("移动失败");
        }
    }

    /**
     * 删除通讯录记录
     * @param request 包含id的JSON参数
     * @return 删除结果
     */
    @PostMapping("/delContacts")
    public CommonResult<?> delContacts(@RequestBody BatchDeleteRequest request) {

        // 从JSON参数中获取id
        List<Integer> ids = request.getIds();
        if (ids == null || ids.isEmpty()) {
            return CommonResult.failed("请选择要删除的通讯录记录");
        }

        contactsService.batchDelContact(ids);
        return CommonResult.success("删除通讯录成功");
    }
}
